namespace Sidvall.Business
{
    public sealed class DataPortalContextManager
    {
        #region Public Members

        #region AllowDataRowStateIsDeleted

        private bool _AllowDataRowStateIsDeleted = false;
        public bool AllowDataRowStateIsDeleted
        {
            get
            {
                return _AllowDataRowStateIsDeleted;
            }
            set
            {
                _AllowDataRowStateIsDeleted = value;
            }
        }

        #endregion
        #region AllowRelations

        private bool _AllowRelations = false;
        public bool AllowRelations
        {
            get
            {
                return _AllowRelations;
            }
            set
            {
                _AllowRelations = value;
            }
        }

        #endregion

        #region EnsureIsValid

        public void EnsureIsValid(Sidvall.Business.IDataPortalContext context)
        {
            if (context == null)
                return;
            if (!this.AllowDataRowStateIsDeleted)
            {
                if (context.DataRowStateIsDeleted() == true)
                    throw new Sidvall.Security.UnauthorizedException();
            }
            if (!this.AllowRelations)
            {
                if (context.RelationItemsProvider?.RelationItems != null)
                    context.RelationItemsProvider.RelationItems.Clear();
            }
        }

        #endregion

        #endregion
        #region Constructors

        public DataPortalContextManager()
        {
        }

        #endregion
    }
}
